<template>
  <div class="roles">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '../welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card">
      <el-button type="primary" @click="addDia = true"> 添加角色</el-button>
      <!-- 添加的弹框 -->

      <el-dialog title="添加用户" :visible.sync="addDia" width="50%">
        <el-form
          :model="addRolesForm"
          ref="addRolesRef"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="角色名" prop="username">
            <el-input v-model="addRolesForm.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" prop="password">
            <el-input v-model="addRolesForm.roleDesc"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="addDia = false">取 消</el-button>
          <el-button type="primary" @click="addBtn">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 表格 -->
      <el-table :data="roles" border style="width: 100%">
        <el-table-column prop="date" label="" type="expand"> </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="roleName" label="角色名"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>

        <el-table-column fixed="right" label="操作" width="400">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              @click="Edit(scope.row)"
              
              >编辑</el-button
            >

            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="del(scope.row.id)"
              >删除</el-button
            >
            <!-- 分配权限 -->
            <el-button type="warning" icon="el-icon-star-off" size="small"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 编辑弹框 -->
    <el-dialog title="编辑用户" :visible.sync="editDia" width="50%">
      <el-form
        :model="editRolesForm"
        :rules="editRolesRules"
        ref="editRolesRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名" prop="roleName">
          <el-input v-model="editRolesForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editRolesForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDia = false">取 消</el-button>
        <el-button type="primary" @click="editBtnOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRolesList,
  addRolesApi,
  RloesDelApi,
  RolesEditApi,
} from "@/http/api";
export default {
  data() {
    return {
      roles: [],
      addDia: false,
      editDia: false,
      addRolesForm:{}, //绑定的双向数据
      editRolesRules: {
        roleName: [
          { required: true, message: "请输入名字", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15个字符",
            trigger: "change",
          },
        ],
        roleDesc: [
          { required: true, message: "请输入描述", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "change",
          },
        ],
      },
      editRolesForm:{
        roleName:'',
        roleDesc:""
      }
    };
  },
  created() {
    this.getRolesList();
  },
  methods: {
    // 渲染列表
    async getRolesList() {
      const res = await getRolesList();
      console.log(res);
      this.roles = res;
      // this.roles = roles;
    },
    // 添加
    addBtn() {
      this.$refs.addRolesRef.validate(async (valid) => {
        if (!valid) return false;

        await addRolesApi(this.addRolesForm);
        this.addDia = false;
        this.getRolesList();
      });
    },
    //编辑
    Edit(row) {
      this.addRolesForm = row;
      this.editDia = true;
    },
    // 编辑完成
    editBtnOk() {
      this.$refs.editRolesRef.validate(async (valid) => {
        if (!valid) return false;
        const { id, roleName, roleDesc } = this.editRolesForm;
        await RolesEditApi(id, { roleName, roleDesc });
        this.editDia = false;
        this.getRolesList();
      });
    },
    // 删除
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await RloesDelApi(id);
          this.getRolesList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  components: {},
};
</script>

<style lang='scss'>
.box-card {
  margin-top: 10px;
}
.el-table {
  margin-top: 10px;
}
.el-button {
  margin: 0;
}
</style>
